package com.bocai.kelalacar.ui.car.main.act;

import android.content.Intent;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.CarDriverBean;
import com.bocai.kelalacar.bean.CheckLineBean;
import com.bocai.kelalacar.bean.MyAddressBean;
import com.bocai.kelalacar.ui.car.main.ada.SpinnerCarAdapter;
import com.bocai.kelalacar.ui.car.main.contract.PublishInformationContract;
import com.bocai.kelalacar.ui.car.main.module.PublishInformationModule;
import com.bocai.kelalacar.ui.car.main.presenter.PublishInformationPresenter;
import com.bocai.kelalacar.ui.car.set.act.AddCarActivity;
import com.bocai.kelalacar.ui.car.set.act.AddDriverActivity;
import com.bocai.kelalacar.ui.car.set.act.CarTeamsActivity;
import com.bocai.kelalacar.ui.car.set.act.DriverMangerActivity;
import com.bocai.kelalacar.util.DateUtil;
import com.bocai.kelalacar.util.SP;
import com.bocai.kelalacar.widget.SpinnerDialog;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yuanfei on 2017/6/20.
 */

public class PublishInformationActivity extends BaseActivity implements PublishInformationContract.view{
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_select_car)
    TextView tvSelectCar;
    @BindView(R.id.ll_select_car)
    LinearLayout llSelectCar;
    @BindView(R.id.tv_select_driver)
    TextView tvSelectDriver;
    @BindView(R.id.ll_select_driver)
    LinearLayout llSelectDriver;
    @BindView(R.id.tv_start_data)
    TextView tvStartData;
    @BindView(R.id.ll_start_data)
    LinearLayout llStartData;
    @BindView(R.id.tv_end_data)
    TextView tvEndData;
    @BindView(R.id.ll_end_data)
    LinearLayout llEndData;
    @BindView(R.id.check_agree)
    CheckBox checkAgree;
    @BindView(R.id.btn_publish)
    AppCompatButton btnPublish;
    List<String> list = new ArrayList<>();
    SpinnerDialog spinnerDialog;
    @Inject
    PublishInformationPresenter presenter;
    List<CarDriverBean.ContentBean.CarListBean> carListBeen = new ArrayList<>();
    List<CarDriverBean.ContentBean.DriverListBean> driverListBeen = new ArrayList<>();
    TimePickerView pickerView;
    private int index = 0;
    CheckLineBean bean;
    String priceId;
    String carId;
    String driverId;
    Date startDate;
    Date endDate;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_cat_teams)
    TextView tvCatTeams;

    @Override
    public int getLayoutId() {
        return R.layout.act_publish_information;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new PublishInformationModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("发布行程");
        initBack(imgBack,llBack);
        setDataView();
        bean = (CheckLineBean) getIntent().getSerializableExtra("data");
        presenter.getCarDriverList(SP.get(this,"id","").toString());

    }
    @OnClick({R.id.ll_select_car,R.id.ll_select_driver,R.id.ll_start_data,R.id.ll_end_data,R.id.btn_publish,R.id.tv_cat_teams})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.ll_select_car:
                new SpinnerDialog(this).builder().setCarList(carListBeen, new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if (position == 0){

                        }else if (position == carListBeen.size() + 1){
                            startActivityForResult(new Intent(PublishInformationActivity.this, AddCarActivity.class),123);
//                            startActivity(AddCarActivity.class);
                        }else {
                            position = position - 1;
                            boolean isTrue = false;
                            for (int i = 0;i<bean.getPriceList().size();i++){
                                if (bean.getPriceList().get(i).getSeat() == carListBeen.get(position).getSeats() ){
                                    isTrue = true;
                                    priceId = bean.getPriceList().get(i).getPriceId();
                                }
                            }
                            if (isTrue) {
                                carId = carListBeen.get(position).getCarId();
                                tvSelectCar.setText(carListBeen.get(position).getCarLicense() + "   " + carListBeen.get(position).getSeats() + "座");
                                if (!TextUtils.isEmpty(carId)&&!TextUtils.isEmpty(driverId)&&
                                        !TextUtils.isEmpty(tvStartData.getText().toString())&&!TextUtils.isEmpty(tvEndData.getText().toString())){
                                    btnPublish.setEnabled(true);
                                    btnPublish.setTextColor(getResources().getColor(R.color.color_white));
                                }else {
                                    btnPublish.setEnabled(false);
                                    btnPublish.setTextColor(getResources().getColor(R.color.text_color));
                                }
                            }else{
                                showErrorTip("请联系后台添加相对于的车位数");
                            }
                        }

                    }
                }).show();
                break;
            case R.id.ll_select_driver:
                new SpinnerDialog(this).builder().setDriverList(driverListBeen, new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Log.e("onItemClick",position+"   "+driverListBeen.size());
                        if (position == 0){

                        }else if (position == driverListBeen.size() + 1){
                            startActivityForResult(new Intent(PublishInformationActivity.this, DriverMangerActivity.class),123);
//                            startActivity(AddDriverActivity.class);
                        }else {
                            position = position - 1;
                            driverId = driverListBeen.get(position).getDriverId();
                            tvSelectDriver.setText(driverListBeen.get(position).getDriverName());
                            if (!TextUtils.isEmpty(carId)&&!TextUtils.isEmpty(driverId)&&
                                    !TextUtils.isEmpty(tvStartData.getText().toString())&&!TextUtils.isEmpty(tvEndData.getText().toString())){
                                btnPublish.setEnabled(true);
                                btnPublish.setTextColor(getResources().getColor(R.color.color_white));
                            }else {
                                btnPublish.setEnabled(false);
                                btnPublish.setTextColor(getResources().getColor(R.color.text_color));
                            }
                        }
                    }
                }).show();
                break;
            case R.id.ll_start_data:
                index = 1;
                pickerView.show();
                break;
            case R.id.ll_end_data:
                index = 2;
                pickerView.show();
                break;
            case R.id.btn_publish:
                if (TextUtils.isEmpty(carId)){
                    showErrorTip("请选择车辆");
                }else if(TextUtils.isEmpty(driverId)){
                    showErrorTip("请选择司机");
                }else if(TextUtils.isEmpty(tvStartData.getText().toString())){
                    showErrorTip("请选择用车开始时间");
                }else if(TextUtils.isEmpty(tvEndData.getText().toString())){
                    showErrorTip("请选择用车结束时间");
                }else if(!checkAgree.isChecked()){
                    showErrorTip("请阅读并同意服务协议");
                }else if (startDate.after(endDate)){
                    showErrorTip("出发时间不晚于最晚出发时间");
                }else {
                    presenter.teamPublish(priceId,carId,driverId,tvStartData.getText().toString(),tvEndData.getText().toString());
                }
                break;
            case R.id.tv_cat_teams:
                startActivity(CarTeamsActivity.class);
                break;

        }
    }

    public void setDataView() {
        pickerView = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                if (index == 1) {

                    startDate = date;
                    tvStartData.setText(DateUtil.dateToDateTimeString(date));
                }else if (index == 2){
//                    if (date.getTime() - new Date().getTime() <= 6*60*60*1000){
//                        showErrorTip("出行时间小于6小时");
//                    }else {
                        endDate = date;
                        tvEndData.setText(DateUtil.dateToDateTimeString(date));
//                    }
                }
                if (!TextUtils.isEmpty(carId)&&!TextUtils.isEmpty(driverId)&&
                !TextUtils.isEmpty(tvStartData.getText().toString())&&!TextUtils.isEmpty(tvEndData.getText().toString())){
                    btnPublish.setEnabled(true);
                    btnPublish.setTextColor(getResources().getColor(R.color.color_white));
                }else {
                    btnPublish.setEnabled(false);
                    btnPublish.setTextColor(getResources().getColor(R.color.text_color));
                }
            }
        })
                .setType(TimePickerView.Type.YEAR_MONTH_DAY_HOUR_MIN)
                .build();
    }

    @Override
    public void getCarDriverList(CarDriverBean bean) {
        carListBeen = bean.getContent().getCarList();
        driverListBeen = bean.getContent().getDriverList();
    }

    @Override
    public void checkLineSuccess(CheckLineBean bean) {

    }

    @Override
    public void teamPublishSuccess() {
        showErrorTip("发布成功");
        setResult(11);
        startActivity(MainActivity.class);
        finish();
    }

    @Override
    public void originAreasList(List<MyAddressBean> list) {

    }

    @Override
    public void arriveAreasList(List<MyAddressBean> list) {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 123){
            presenter.getCarDriverList(SP.get(this,"id","").toString());
        }
    }
}
